﻿using System;
using System.ComponentModel;
using System.Linq;

namespace CardPlay.Model.Primitives
{
    public static class ExtensionsToEnum
    {
        public static string ToLocalizedString(this Enum @enum)
        {
            if (@enum == null) return null;

            var description = @enum.ToString();
            var fieldInfo = @enum.GetType().GetField(description);
            var attribute = fieldInfo.GetCustomAttributes(typeof(DescriptionAttribute), false).Cast<DescriptionAttribute>().FirstOrDefault();

            return attribute != null ? attribute.Description : description.SplitIntoWords();
        }
    }
}